package com.tuni.birthdaymoney.web.controller;

import java.security.Principal;
import java.util.Map;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.tuni.birthdaymoney.persistence.entity.Person;
import com.tuni.birthdaymoney.service.OperationService;
import com.tuni.birthdaymoney.service.PersonService;
import com.tuni.birthdaymoney.service.UserService;
import com.tuni.birthdaymoney.service.util.web.DateTimeEditor;
import com.tuni.birthdaymoney.web.model.MasterPage;
import com.tuni.birthdaymoney.web.model.OperationHistoryPage;

@Controller
public class OperationHistoryController {
    @Autowired
    private UserService userService;
    @Autowired
    private OperationService operationService;
    @Autowired
    private PersonService personService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        String dateFormat = "dd.MM.yyyy";
        binder.registerCustomEditor(DateTime.class, new DateTimeEditor(dateFormat, true));
    }

    @RequestMapping(value = "/operations", method = RequestMethod.GET)
    public String displayAll(Principal principal, Map<String, Object> operationHistoryPageMap) {
        MasterPage masterPageModel = new MasterPage(userService.getUserByEmail(principal.getName()), false);
        operationHistoryPageMap.put("masterPageModel", masterPageModel);
        OperationHistoryPage operationHistoryPageModel = new OperationHistoryPage();
        operationHistoryPageModel.setOperations(operationService.findAllOrderedByDate());
        operationHistoryPageModel.setHumanResources(new Person());
        operationHistoryPageModel.setReceiptor(new Person());
        operationHistoryPageModel.setDonator(new Person());
        operationHistoryPageModel.setAllYears(operationService.getAllYears());
        operationHistoryPageModel.setAllPeople(personService.getAllPeople());
        operationHistoryPageMap.put("operationHistoryPageModel", operationHistoryPageModel);
        return "operations";
    }

    @RequestMapping(value = "/operations", method = RequestMethod.POST)
    public String displayFiltered(@ModelAttribute("operationHistoryPageModel") OperationHistoryPage operationHistoryPageModel,
            BindingResult result, Principal principal, Map<String, Object> operationHistoryPageMap) {
        if (!result.hasErrors()) {
            MasterPage masterPageModel = new MasterPage(userService.getUserByEmail(principal.getName()), false);
            operationHistoryPageMap.put("masterPageModel", masterPageModel);
            operationHistoryPageModel.setOperations(operationService.findAllOrderedByDate(operationService
                    .prepareBooleanExpresion(operationHistoryPageModel)));
            operationHistoryPageModel.setAllYears(operationService.getAllYears());
            operationHistoryPageModel.setAllPeople(personService.getAllPeople());
            operationHistoryPageMap.put("operationHistoryPageModel", operationHistoryPageModel);
        }

        return "operations";
    }

}